package com.zy.openchat.util;

import com.zy.openchat.core.manager.TelegramManager;
import com.zy.openchat.core.model.message.ChatInfo;

import java.util.Comparator;

public class ChatComparator implements Comparator<ChatInfo> {

    @Override
    public int compare(ChatInfo ct1, ChatInfo ct2) {
        int result = (ct1.unread_count>0?1:0)-(ct2.unread_count>0?1:0);
        if(result!=0){
            result = result > 0 ? -1 : 7;
        }else{
            if(ct1.unread_count>0&&ct2.unread_count>0){
                result = (Long.compare(ct2.modifyDate(), ct1.modifyDate()))>0?6:-2;
            }else{
                result = (ct1.id == TelegramManager.TG_USERID_SYSTEM_NOTICE?1:0)-(ct2.id == TelegramManager.TG_USERID_SYSTEM_NOTICE?1:0);
                if(result!=0){
                    result = result > 0 ? -3 : 5;
                }else{
                    result = (ct1.is_pinned?1:0)-(ct2.is_pinned?1:0);
                    if(result!=0){
                        result = result > 0 ? -4 : 4;
                    }else{
                        result = (ct1.isSecretChat()?1:0)-(ct2.isSecretChat()?1:0);
                        if(result!=0){
                            result = result > 0 ? -5 : 3;
                        }else{
                            result = (Long.compare(ct2.modifyDate(), ct1.modifyDate()))>0?2:-6;
                            if(ct2.modifyDate() > ct1.modifyDate()){
                                result =2;
                            }else if(ct2.modifyDate() < ct1.modifyDate()){
                                result = -6;
                            }else {
                                result = 0;
                            }
                        }
                    }
                }
            }
        }
        return result;
    }
}
